package com.ruoyi.system.service.impl.trade;

import com.ruoyi.system.domain.db.trade.TradeMaterialUsage;
import com.ruoyi.system.domain.dto.MaterialUsageDto;
import com.ruoyi.system.domain.vo.MaterialUsageVo;
import com.ruoyi.system.mapper.trade.TradeMaterialUsageMapper;
import com.ruoyi.system.service.ITradeMaterialUsageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class TradeMaterialUsageServiceImpl implements ITradeMaterialUsageService {


    @Resource
    private TradeMaterialUsageMapper tradeMaterialUsageMapper;
    /**
     * 根据id查询物资使用情况
     *
     * @param materialId
     */
    @Override
    public List<MaterialUsageVo> selectMaterialUsageByOrderId(Long materialId) {
        return tradeMaterialUsageMapper.selectTradeMaterialUsageById(materialId).stream().map(this::dtoToVo).collect(Collectors.toList());
    }

    /**
     * 查询所有物资使用情况
     */
    @Override
    public List<MaterialUsageVo> selectMaterialUsageList() {
        return tradeMaterialUsageMapper.selectAllTradeMaterialUsage().stream().map(this::dtoToVo).collect(Collectors.toList());
    }

    /**
     * 新增物资使用情况
     *
     * @param materialUsage
     */
    @Override
    public int insertMaterialUsage(TradeMaterialUsage materialUsage) {
        return 0;
    }

    public MaterialUsageVo dtoToVo(MaterialUsageDto dto)
    {
        MaterialUsageVo materialUsageVo = new MaterialUsageVo();
        materialUsageVo.setMaterialId(dto.getMaterial().getMaterialId());
        materialUsageVo.setMaterialName(dto.getMaterial().getMaterialName());
        materialUsageVo.setMaterialTypeName(dto.getMaterial().getMaterialTypeName());
        materialUsageVo.setApplicationType(dto.getMaterial().getApplicationType());
        materialUsageVo.setQuantity(dto.getQuantity());
        materialUsageVo.setUserId(dto.getCreateUser().getUserId());
        materialUsageVo.setUserName(dto.getCreateUser().getUserName());
        materialUsageVo.setPostId(dto.getSysPost().getPostId());
        materialUsageVo.setPostName(dto.getSysPost().getPostName());
        materialUsageVo.setCreateTime(dto.getCreateTime());
        return materialUsageVo;
    }
}
